//
//  XMGFirstPageTVc.m
//  weibo
//
//  Created by DP on 16/7/3.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "XMGFirstPageTVc.h"
#import "GetWeiboAccount.h"
#import "WeiboLogin.h"
#import "WBAccount.h"
#import "XMGFirstPageCell.h"
#import <AFNetworking.h>
#import "YYHttpTool.h"
#import "XMGFirstPageItem.h"


@interface XMGFirstPageTVc ()
@property (nonatomic, strong) WBAccount *account;
/**网络数据*/
@property (nonatomic, strong) NSMutableArray *statuses;
@end

static NSString *ID = @"FirstPageCell";
@implementation XMGFirstPageTVc
//懒加载
- (NSMutableArray *)statuses {
    if (!_statuses ) {
        
        _statuses = [NSMutableArray array];
    }
    return _statuses;
}

- (void)viewDidLoad {
    [super viewDidLoad];
       [self.tableView registerNib:[UINib nibWithNibName:@"XMGFirstPageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ID];
//  
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 200;
    
    if (_account == nil) {
        WBAccount *account = [GetWeiboAccount getAccount];
        self.account = account;
    }else {
        
        [WeiboLogin weiboLogin];
    }

    [self get];
}
//获取网络数据
- (void)get {
    
    NSDictionary *dict = @{@"access_token":_account.wbToken};
    [YYHttpTool get:@"https://api.weibo.com/2/statuses/friends_timeline.json" params:dict success:^(id responseObj) {
        NSString *str  = [[NSString alloc]initWithData:responseObj encoding:NSUTF8StringEncoding];

        NSString *nstr = [str stringByReplacingOccurrencesOfString:@"null" withString:@"\"\"" options:0 range:NSMakeRange(0, str.length)];

        NSData *data = [nstr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
        
        NSDictionary *nDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        //将获取到的网路数据转换成模型数据存储
        for (NSDictionary *dict in nDict[@"statuses"]) {
            XMGFirstPageItem *item = [XMGFirstPageItem firstPageItemDict:dict];
            [self.statuses addObject:item];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });

        [nDict writeToFile:@"/Users/Tommy/Desktop/p1.plist" atomically:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
   
   }




#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XMGFirstPageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
      cell.item = self.statuses[indexPath.row];
             return cell;
}

/**xib 和 storyboard设置好约束，不需要计算高度，纯代码才需要*/
/*
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static XMGFirstPageCell *cell;
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:ID];
         cell.item = self.statuses[indexPath.row];
    }
    return  [cell updateHeight];
}
*/
@end
